import { BaseActions, IActionState } from "./global-state";

/**
 * 微应用处理器(处理基座应用通知微应用处理行为)
 */
export class MicroAppHandler {
  /**
   * MicroAppHandler对象
   *
   */
  private static MicroAppHandler: MicroAppHandler;

  /**
   * 获取MicroAppHandler实例
   *
   */
  public static getInstance(): MicroAppHandler {
    if (!this.MicroAppHandler) {
      this.MicroAppHandler = new MicroAppHandler();
    }
    return this.MicroAppHandler;
  }

  /**
   * 处理微应用行为
   * @param actionState
   */
  public handleAction(actionState: IActionState) {
    const { action } = actionState;
    switch (action) {
      case BaseActions.LOGOUT:
        this.handleLogout(actionState);
        break;
      case BaseActions.MENUITEMCLICK:
        this.handleMenuItemClick(actionState);
        break;
      default:
        console.log(`%c${action}暂未实现`,'color:green');
        break;
    }
  }

  /**
   * 处理登出
   * @param actionState
   */
  public handleLogout(actionState: IActionState) {}

  /**
   * 处理菜单点击
   * @param actionState
   */
  public handleMenuItemClick(actionState: IActionState) {
    const { data } = actionState;
    App.getAppFuncHelper().execute(data.funcTag);
  }
}
